package com.ebpm.designer.xml
{
	import com.ebpm.designer.NodeContainer;
	import com.ebpm.designer.WF;
	import com.ebpm.designer.WorkPanel;
	import com.ebpm.designer.item.Item;
	import com.ebpm.designer.line.DDLine;
	import com.ebpm.designer.line.DLLine;
	import com.ebpm.designer.line.DRLine;
	import com.ebpm.designer.line.DTLine;
	import com.ebpm.designer.line.LDLine;
	import com.ebpm.designer.line.LLLine;
	import com.ebpm.designer.line.LRLine;
	import com.ebpm.designer.line.LTLine;
	import com.ebpm.designer.line.Line;
	import com.ebpm.designer.line.RDLine;
	import com.ebpm.designer.line.RLLine;
	import com.ebpm.designer.line.RRLine;
	import com.ebpm.designer.line.RTLine;
	import com.ebpm.designer.line.TDLine;
	import com.ebpm.designer.line.TLLine;
	import com.ebpm.designer.line.TRLine;
	import com.ebpm.designer.line.TTLine;
	import com.ebpm.designer.mainLayout.variable.VariablePanel;
	
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;

	public class FileParser
	{
		
		private static var  file:FileReference = new FileReference();
		private static var postions:ArrayList = new ArrayList();																		//【节点】和【线】的位置信息
		
		public function FileParser() {}
		
		public static function loadDefineFile2WorkPanel():void
		{
			var filter:FileFilter = new FileFilter("提示文件类型*.*", "*.zip;*.xml");											//只读取文件类型为【zip】或【xml】
			file.addEventListener(Event.SELECT, selectedFile);
			file.browse([filter]);
		}
		
		public static function selectedFile(e:Event):void{
			if(e.target.size > 1024*1024*1024 * 10){																								//如果流程超过10M，提示错误
				Alert.show("文件超过指定大小[10M] - 请重新选择!");
				return;
			}
			file.load();
			file.addEventListener(Event.COMPLETE, _complete);
			file.addEventListener(Event.CANCEL, _cancel);
		}
		
		public static function _cancel(e:Event):void{																							//取消时，将焦点重新设置回WF.wp
			WF.wp.setFocus();
		}
		
		public static function _complete(e:Event):void{																						//解析文件，加载流程
			//1、删除现有的WF.wp - 新建【WorkPanel】
			WF.app.removeElement(WF.wp);
			var wp:WorkPanel = new WorkPanel(WF.app);
			WF.app.addElement(wp);
			
			var fileName:String = file.name;
			var fileData:ByteArray = file.data;
			if(fileName.indexOf(".zip") >= 0) {																											//如果是zip压缩包，解压，读取xml文件
				var zipFile:ZipFile = new ZipFile(file.data);
				var arrayTemp:Array = zipFile.entries;
				var xmlFileName:String;
				for(var i:Number = 0; i<arrayTemp.length; i++) {
					xmlFileName = arrayTemp[i];
					if(xmlFileName.indexOf(".xml") >= 0) {
						break;
					}
				}
				var zipEntry:ZipEntry  = zipFile.getEntry(xmlFileName);
				fileData = zipFile.getInput(zipEntry);
			}
			
			
			fileData.position = 0;
			var xml:String = null;
			xml = fileData.readUTFBytes(fileData.length);																						//根据UTF-8读取文件
			var definitionsStr:String = xml.substr(0, xml.indexOf("<Position>"));
			var definitions:XML = new XML(definitionsStr);
			
			var positionsStr:String = xml.substring(xml.indexOf("<Position>"), xml.indexOf("</Position>") + 11);
			initPosition(positionsStr);
			
			var list:XMLList = definitions.children();
			var temp:XML = null;
			for each(temp in list) {																																	//初始化所有【节点】信息
				var p:Position = getPosition(temp.@id);
				var node:NodeContainer = null;
				if(temp.name() == "StartEvent") {
					node = new NodeContainer(p.x, p.y, WF.wp, "StartEvent");
					node.variable.processKey = definitions.@key;
					node.variable.processDesc = definitions.@desc;
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "SubStartEvent") {
					node = new NodeContainer(p.x, p.y, WF.wp, "SubStartEvent");
					node.variable.processKey = temp.@subKey;
					node.variable.processDesc = temp.@subDesc;
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "UserTask") {
					node = new NodeContainer(p.x, p.y, WF.wp, "UserTask", p.w, p.h);
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.variable.user = temp.@candidateUsers;
					node.variable.roler = temp.@candidateGroups;
					node.variable.org = temp.@candidateOrgs;
					node.variable.filter = temp.@taskFilter;
					node.variable.listener = temp.@taskListener;
					node.variable.assignee = temp.@assignee;
					node.mission.txtLabel.text = node.variable.desc;
				}
				if(temp.name() == "LaneBefore") {
					node = new NodeContainer(p.x, p.y, WF.wp, "LaneBefore", p.w, p.h);
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.variable.user = temp.@candidateUsers;
					node.variable.roler = temp.@candidateGroups;
					node.variable.org = temp.@candidateOrgs;
					node.variable.filter = temp.@taskFilter;
					node.variable.listener = temp.@taskListener;
					node.variable.assignee = temp.@assignee;
				}
				if(temp.name() == "ForkTask") {
					node = new NodeContainer(p.x, p.y, WF.wp, "ForkTask", p.w, p.h);
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.variable.user = temp.@candidateUsers;
					node.variable.roler = temp.@candidateGroups;
					node.variable.org = temp.@candidateOrgs;
					node.variable.filter = temp.@taskFilter;
					node.variable.listener = temp.@taskListener;
					node.variable.assignee = temp.@assignee;
					node.mission.txtLabel.text = node.variable.desc;
				}
				if(temp.name() == "ServiceTask") {
					node = new NodeContainer(p.x, p.y, WF.wp, "ServiceTask", p.w, p.h);
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.variable.dueClass = temp.@serviceTaskExecute;
					node.variable.filter = temp.@taskFilter;
					node.variable.listener = temp.@taskListener;
					node.mission.txtLabel.text = node.variable.desc;
				}
				if(temp.name() == "EndEvent") {
					node = new NodeContainer(p.x, p.y, WF.wp, "EndEvent");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.mission.txtLabel.text = node.variable.desc;
				}
				if(temp.name() == "SubEndEvent") {
					node = new NodeContainer(p.x, p.y, WF.wp, "SubEndEvent");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "ForkBefore") {
					node = new NodeContainer(p.x, p.y, WF.wp, "ForkBefore");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "ForkAfter") {
					node = new NodeContainer(p.x, p.y, WF.wp, "ForkAfter");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "LaneAfter") {
					node = new NodeContainer(p.x, p.y, WF.wp, "LaneAfter");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
				}
				if(temp.name() == "LaneTask") {
					node = new NodeContainer(p.x, p.y, WF.wp, "LaneTask", p.w, p.h);
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.variable.filter = temp.@taskFilter;
					node.variable.listener = temp.@taskListener;
					node.mission.txtLabel.text = node.variable.desc;
				}
				if(temp.name() == "LaneForkTask") {
					node = new NodeContainer(p.x, p.y, WF.wp, "LaneForkTask");
					node.variable.name = temp.@id;
					node.variable.desc = temp.@name;
					node.mission.txtLabel.text = node.variable.desc;
				}
			}
			
			for each(temp in list) {																										//初始化所有【线】信息
				if(temp.name() == "SequenceFlow") {
					var name:String = temp.@id;;																				//id信息
					var desc:String = temp.@desc;;																				//描述
					var express:String = temp.children().text();														//表达式
					var sourceNodeName:String = temp.@sourceRef;											//源节点
					var targetNodeName:String = temp.@targetRef;												//目标节点
					var sourceNodeContainer:NodeContainer = null;															
					var targetNodeContainer:NodeContainer = null;
					var pTemp:Position = getPosition(name);															//获取线的位置信息
					
					var line:Line = null;
					for(var ii:Number=0; ii<WF.wp.nodeContainers.length; ii++) {					//获取【源节点】和【目标节点】
						var n:NodeContainer = WF.wp.nodeContainers.getItemAt(ii) as NodeContainer;
						if(n.variable.name == sourceNodeName) {
							sourceNodeContainer = n;
						}
						if(n.variable.name == targetNodeName) {
							targetNodeContainer = n;
						}
					}
					
					var lineType:String = pTemp.type;																				//线类型
					var fromItem:Item = null;
					var toItem:Item = null;
					
					if(lineType == "DDLine") {
						fromItem = sourceNodeContainer.downItem;
						toItem = targetNodeContainer.downItem;
						line = new DDLine();
					}
					if(lineType == "DLLine") {
						fromItem = sourceNodeContainer.downItem;
						toItem = targetNodeContainer.leftItem;
						line = new DLLine();
					}
					if(lineType == "DRLine") {
						fromItem = sourceNodeContainer.downItem;
						toItem = targetNodeContainer.rightItem;
						line = new DRLine();
					}
					if(lineType == "DTLine") {
						fromItem = sourceNodeContainer.downItem;
						toItem = targetNodeContainer.topItem;
						line = new DTLine();
					}
					
					if(lineType == "LDLine") {
						fromItem = sourceNodeContainer.leftItem;
						toItem = targetNodeContainer.downItem;
						line = new LDLine();
					}
					if(lineType == "LLLine") {
						fromItem = sourceNodeContainer.leftItem;
						toItem = targetNodeContainer.leftItem;
						line = new LLLine();
					}
					if(lineType == "LRLine") {
						fromItem = sourceNodeContainer.leftItem;
						toItem = targetNodeContainer.rightItem;
						line = new LRLine();
					}
					if(lineType == "LTLine") {
						fromItem = sourceNodeContainer.leftItem;
						toItem = targetNodeContainer.topItem;
						line = new LTLine();
					}
					
					if(lineType == "RDLine") {
						fromItem = sourceNodeContainer.rightItem;
						toItem = targetNodeContainer.downItem;
						line = new RDLine();
					}
					if(lineType == "RLLine") {
						fromItem = sourceNodeContainer.rightItem;
						toItem = targetNodeContainer.leftItem;
						line = new RLLine();
					}
					if(lineType == "RRLine") {
						fromItem = sourceNodeContainer.rightItem;
						toItem = targetNodeContainer.rightItem;
						line = new RRLine();
					}
					if(lineType == "RTLine") {
						fromItem = sourceNodeContainer.rightItem;
						toItem = targetNodeContainer.topItem;
						line = new RTLine();
					}
					
					if(lineType == "TDLine") {
						fromItem = sourceNodeContainer.topItem;
						toItem = targetNodeContainer.downItem;
						line = new TDLine();
					}
					if(lineType == "TLLine") {
						fromItem = sourceNodeContainer.topItem;
						toItem = targetNodeContainer.leftItem;
						line = new TLLine();
					}
					if(lineType == "TRLine") {
						fromItem = sourceNodeContainer.topItem;
						toItem = targetNodeContainer.rightItem;
						line = new TRLine();
					}
					if(lineType == "TTLine") {
						fromItem = sourceNodeContainer.topItem;
						toItem = targetNodeContainer.topItem;
						line = new TTLine();
					}
					
					line.resizeFromExtraX = pTemp.resizeFromExtraX;
					line.resizeFromExtraY = pTemp.resizeFromExtraY;
					line.resizeToExtraX = pTemp.resizeToExtraX;
					line.resizeToExtraY = pTemp.resizeToExtraY;
					if(pTemp.resizeMiddleExtra != 0) {
						line.isAlreadyResize = true;
					}
					line.resizeMiddleExtra = pTemp.resizeMiddleExtra;
					line.paint(fromItem.getPageX(), fromItem.getPageY(), toItem.getPageX(), toItem.getPageY());
					line.sourceItem = fromItem;
					line.targetItem = toItem;
					fromItem.sourceLines.addItem(line);
					toItem.targetLines.addItem(line);
					line.distanceWithEndPointWidth = pTemp.lineTxtDistanceWidth;
					line.distanceWithEndPointHeight = pTemp.lineTxtDistanceHeight;
					line.variable.name = name;
					line.variable.desc = desc;
					line.variable.express = express;
					line.txtLabel.text = desc;
					line.showLabelByParserFile();																																			//显示【线】文本信息
				}
			}
			postions.removeAll();
			
			VariablePanel.publicX = WF.wp.getMaxContainerPoint().x;
			WF.wp.setFocus();
//			Alert.show(dataStr);
		}
		
		//初始化所有【节点】和【线】的位置信息
		public static function initPosition(positionsStr:String):void {
			var positionXml:XML = new XML(positionsStr);
			var list:XMLList = positionXml.children();
			for each(var temp:XML in list) {
				var p:Position = new Position();
				p.id = temp.@id;
				p.w = temp.@w;
				p.h = temp.@h;
				p.x = temp.@x;
				p.y = temp.@y;
				p.type = temp.@type;
				p.resizeFromExtraX = temp.@resizeFromExtraX;
				p.resizeFromExtraY = temp.@resizeFromExtraY;
				p.resizeMiddleExtra = temp.@resizeMiddleExtra;
				p.resizeToExtraX = temp.@resizeToExtraX;
				p.resizeToExtraY = temp.@resizeToExtraY;
				p.lineTxtDistanceHeight = temp.@distanceHeight;
				p.lineTxtDistanceWidth = temp.@distanceWidth;
				postions.addItem(p);
			}
		}
		
		//根据【id】查询位置信息
		public static function getPosition(id:String):Position {
			for(var i:Number=0; i<postions.length; i++) {
				var p:Position = postions.getItemAt(i) as Position;	
				if(p.id == id) return p;
			}
			return null;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}